#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/completion.h>
#include <linux/kthread.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
#include <linux/debugfs.h>

static int count = 1;
module_param(count, int, S_IRUSR);

#ifdef CONFIG_PROC_FS

struct proc_dir_entry *dbgm_proc_dir;
struct proc_dir_entry *dbgm_file;

static ssize_t my_read_proc_entry(struct file *filep, char __user *buffer,
				  size_t len, loff_t *offset)
{
	const char buf[] = "Hello world from proc!\n";
	u32 buflen;

	buflen = strlen(buf);
	if (buflen <= *offset)
		return 0;

	if (*offset + len > buflen)
		len = buflen - *offset;

	if (copy_to_user(buffer, buf + *offset, len)) {
		pr_err("%s:copy to user failed!\n", __func__);
		return -EFAULT;
	}

	*offset += len;

	return len;
}

// 定义文件操作结构体
static const struct proc_ops my_fops = { .proc_read = my_read_proc_entry };

#endif

static ssize_t dbgm_read(struct file *filp, char __user *buffer, size_t n,
			 loff_t *pos)
{
	char buf[64] = { 0 };
	sprintf(buf, "count is %ld\n", count);
	int data_len = sizeof(buf);

	if (*pos >= data_len)
		return 0;

	if ((*pos + n) > ULLONG_MAX)
		return 0;

	if (*pos + n >= data_len)
		n = data_len - *pos;

	if (copy_to_user(buffer, buf + *pos, (unsigned int)n))
		return -EFAULT;

	*pos = n + *pos;

	return n;
}

static ssize_t dbgm_write(struct file *filp, const char __user *buffer,
			  size_t n, loff_t *pos)
{
	char buf[64] = { 0 };

	if (n > 64)
		return 0;

	if (copy_from_user(buf, buffer, n)) {
		return 0;
	}

	if (kstrtoint(buf, 10, &count))
		return -EINVAL;

	return n;
}

static const struct file_operations dbgm_file_fops = { .owner = THIS_MODULE,
						       .read = dbgm_read,
						       .write = dbgm_write };

static struct dentry *dbg_dir;
static struct dentry *dbg_file;
static void dbgm_dbg_init(void)
{
	dbg_dir = debugfs_create_dir("dbgm_dbg", NULL);
	if ((dbg_dir) == ERR_PTR(-ENODEV))
		pr_warn("Debug FS not initialised, debug info not available\n");

	dbg_file = debugfs_create_file("dbgm_file", 0600, dbg_dir, NULL,
				       &dbgm_file_fops);
	if (dbg_file == ERR_PTR(-ENODEV))
		pr_warn("Debug FS not initialised, debug info not available\n");
}

static void ce_debug_exit(void)
{
	if (dbg_dir != NULL) {
		debugfs_remove_recursive(dbg_dir);
		dbg_dir = NULL;
	}
}

MODULE_PARM_DESC(count, "Test module data, count : default 1");

static int __init dbgm_init(void)
{
	printk(KERN_INFO "printk enter %s\n", __func__);

	pr_debug("pr_debug enter %s\n", __func__);

	dbgm_proc_dir = proc_mkdir("dbgm", NULL);
	if (dbgm_proc_dir == NULL) {
		pr_err("proc_mkdir error");
		return -EIO;
	}

	dbgm_file = proc_create("dbgm_file", 0600, dbgm_proc_dir, &my_fops);
	if (dbgm_file == NULL) {
		pr_err("proc_create error");
		proc_remove(dbgm_proc_dir);
		return -EIO;
	}

	dbgm_dbg_init();
	return 0;
}

static void dbgm_exit(void)
{
	printk(KERN_INFO "printk exit %s\n", __func__);

	pr_debug("pr_debug exit %s\n", __func__);

	proc_remove(dbgm_file);
	proc_remove(dbgm_proc_dir);

	ce_debug_exit();
}

module_init(dbgm_init);
module_exit(dbgm_exit);

MODULE_DESCRIPTION("Module named dbgm.");
MODULE_LICENSE("GPL");
